package cn.highedu.coolsharkhub.controller;

import cn.highedu.coolsharkhub.entity.vo.BannerHomepageVO;
import cn.highedu.coolsharkhub.entity.vo.CategoryVO;
import cn.highedu.coolsharkhub.entity.vo.ProductHomepageVO;
import cn.highedu.coolsharkhub.service.BannerService;
import cn.highedu.coolsharkhub.service.CategoryService;
import cn.highedu.coolsharkhub.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 前台首页控制器
 * 轮播图展示
 * 类别操作
 * 商品展示
 */
@RestController
public class HomepageController {
    Logger logger = LoggerFactory.getLogger(HomepageController.class);

    // 注入BannerService
    @Autowired
    BannerService bannerService;
    // 注入CategoryService
    @Autowired
    CategoryService categoryService;
    // 注入ProductService
    @Autowired
    ProductService productService;

    /**
     * 查询所有轮播图
     * @return 轮播图url列表
     */
    @GetMapping("/home/banners")
    public List<String> findAllBanner() {
        List<BannerHomepageVO> bannerHomepageVOS = bannerService.findAllForHomepage();
        return bannerHomepageVOS.stream().map(BannerHomepageVO :: getUrl).collect(Collectors.toList());
    }

    /**
     * 查询所有商品类别
     * @return 类别列表
     */
    @GetMapping("/home/categories")
    public List<CategoryVO> finAllCategories() {
        return categoryService.findHomepageCategories();
    }

    /**
     * 根据类别ID查询相应的商品
     * @param categoryId 类别ID
     * @return 商品列表
     */
    @GetMapping("/home/{id}/products")
    public List<ProductHomepageVO> findProductsByCategoryId(@PathVariable("id") Integer categoryId) {
        return productService.findAllHomepageProduct(categoryId);
    }
}
